package cn.sany.cterminal.ctrl;

import cn.sany.cterminal.common.CommonReturn;
import cn.sany.cterminal.common.constants.CommonConst;
import cn.sany.cterminal.common.constants.CterminalConst;
import cn.sany.cterminal.entity.request.StatisticQueryParam;
import cn.sany.cterminal.service.StatisticService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * @Project : forklift
 * @Package : cn.sany.forklift.ctrl
 * @Class : ForkliftInfoController
 * @Description : 叉车信息相关ctrl
 * @author : zhouxing
 * @CreateDate : 2020-07-14 17:43:41
 * @version : V0.5
 * @Copyright : 2020 sany.com Inc. All rights reserved.
 * @Reviewed :
 * @UpateLog :    Name    Date    Reason/Contents
 *             ---------------------------------------
 *                ****    ****    ****
 *
 */
@RestController
@RequestMapping(path = "info")
@Slf4j
public class StatisticController {
    @Autowired(required = false)
    StatisticService statisticService;

    /**
     *
     * @Method : getAllForkliftInfo
     * @Description :获取累计电耗信息接口
     * @return : CommonReturn
     * @author : zhouxing
     * @CreateDate : 2020-09-14 09:55:21
     *
     */
    @PostMapping(path = "getElectricityConsum")
    public CommonReturn getElectricityConsum(@RequestBody StatisticQueryParam statisticQueryParam) {
        log.info("访问getElectricityConsum接口，获取累计电耗信息接口,statisticQueryParam:{}",statisticQueryParam);
        CommonReturn commonReturn = new CommonReturn();
        if(statisticQueryParam.getStartTime() == null || statisticQueryParam.getEndTime() == null)
        {
            commonReturn.setResultCode(CommonConst.FAIL_CODE);
            commonReturn.setResultMsg(CommonConst.LACK_PARAM);
            return commonReturn;
        }
        try{
            List result = statisticService.getElectricityConsum(statisticQueryParam);
            commonReturn.setResultCode(CommonConst.SUCCESS_CODE);
            commonReturn.setResultMsg(CommonConst.SUCCESS_DESC);
            commonReturn.setData(result);
        }catch (Exception e){
            log.error("获取累计电耗信息接口出错：",e);
            commonReturn.setResultCode(CommonConst.FAIL_CODE);
            commonReturn.setResultMsg(CommonConst.FAIL_DESC);
        }
       return commonReturn;
    }

    /**
     *
     * @Method : getElectricityConsum
     * @Description : 获取累计工作时长信息接口
     * @param statisticQueryParam :
     * @return : cn.sany.cterminal.common.CommonReturn
     * @author : zhouxing
     * @CreateDate : 2020-09-14 11:40:54
     *
     */
    @PostMapping(path = "getWorkTime")
    public CommonReturn getWorkTime(@RequestBody StatisticQueryParam statisticQueryParam) {
        log.info("访问getWorkTime接口，查询所有叉车信息,statisticQueryParam:{}",statisticQueryParam);
        CommonReturn commonReturn = new CommonReturn();
        if(statisticQueryParam.getStartTime() == null || statisticQueryParam.getEndTime() == null)
        {
            commonReturn.setResultCode(CommonConst.FAIL_CODE);
            commonReturn.setResultMsg(CommonConst.LACK_PARAM);
            return commonReturn;
        }
        try{
            List result = statisticService.getWorkTime(statisticQueryParam);
            commonReturn.setResultCode(CommonConst.SUCCESS_CODE);
            commonReturn.setResultMsg(CommonConst.SUCCESS_DESC);
            commonReturn.setData(result);
        }catch (Exception e){
            log.error("获取累计工作时长信息接口出错：",e);
            commonReturn.setResultCode(CommonConst.FAIL_CODE);
            commonReturn.setResultMsg(CommonConst.FAIL_DESC);
        }
        return commonReturn;
    }
    /**
     *
     * @Method : getDistance
     * @Description :获取累计里程信息接口
     * @param statisticQueryParam :
     * @return : cn.sany.cterminal.common.CommonReturn
     * @author : zhouxing
     * @CreateDate : 2020-09-14 11:48:23
     *
     */
    @PostMapping(path = "getDistance")
    public CommonReturn getDistance(@RequestBody StatisticQueryParam statisticQueryParam) {
        log.info("访问getDistance接口，获取累计里程信息,statisticQueryParam:{}",statisticQueryParam);
        CommonReturn commonReturn = new CommonReturn();
        if(statisticQueryParam.getStartTime() == null || statisticQueryParam.getEndTime() == null)
        {
            commonReturn.setResultCode(CommonConst.FAIL_CODE);
            commonReturn.setResultMsg(CommonConst.LACK_PARAM);
            return commonReturn;
        }
        try{
            List result = statisticService.getDistance(statisticQueryParam);
            commonReturn.setResultCode(CommonConst.SUCCESS_CODE);
            commonReturn.setResultMsg(CommonConst.SUCCESS_DESC);
            commonReturn.setData(result);
        }catch (Exception e){
            log.error("获取累计里程信息接口出错：",e);
            commonReturn.setResultCode(CommonConst.FAIL_CODE);
            commonReturn.setResultMsg(CommonConst.FAIL_DESC);
        }
        return commonReturn;
    }
    /**
     *
     * @Method : getFaultRate
     * @Description : 获取故障率接口
     * @param statisticQueryParam :
     * @return : cn.sany.cterminal.common.CommonReturn
     * @author : zhouxing
     * @CreateDate : 2020-09-14 11:56:31
     *
     */
    @PostMapping(path = "getFaultRate")
    public CommonReturn getFaultRate(@RequestBody StatisticQueryParam statisticQueryParam) {
        log.info("访问getFaultRate接口，获取故障率接口,statisticQueryParam:{}",statisticQueryParam);
        CommonReturn commonReturn = new CommonReturn();
        if(statisticQueryParam.getStartTime() == null || statisticQueryParam.getEndTime() == null)
        {
            commonReturn.setResultCode(CommonConst.FAIL_CODE);
            commonReturn.setResultMsg(CommonConst.LACK_PARAM);
            return commonReturn;
        }
        try{
            List result = statisticService.getFaultRate(statisticQueryParam);
            commonReturn.setResultCode(CommonConst.SUCCESS_CODE);
            commonReturn.setResultMsg(CommonConst.SUCCESS_DESC);
            commonReturn.setData(result);
        }catch (Exception e){
            log.error("获取故障率接口出错：",e);
            commonReturn.setResultCode(CommonConst.FAIL_CODE);
            commonReturn.setResultMsg(CommonConst.FAIL_DESC);
        }
        return commonReturn;
    }
}
